<?php
namespace app\zb\controller;

use Jssdk\JSSDK;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\View;

class Index extends Common
{
    private $obj;
    public function _initialize()
    {
        parent::_initialize();
        $this->obj = model('Category');
    }
    public function index() {
        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);

        }

        $info = model('zb_video')->order('create_time desc')->select();
        $this->assign('info', $info);
        return view();
    }

    public function detail() {
        $video_id = input('param.video_id');//视频id
        $openid_id = session('openid_id');//用户openid
        $pagesize = 5;//每页条数

        $video_info = model('zb_video')->get(['id'=>$video_id]);//视频信息
        if (!$video_info) {
            $this->error('视频资源不存在');
        }

		model('zb_video')->where(['id'=>$video_id])->setInc('play_count',1);//播放次数加1

        //$user_info = model('member')->get(['id'=>$member_id]);//用户信息

        $comment = model('zb_comment')->where(['video_id'=>$video_id])->count();//评论数


        $this->assign('page',2);
        $this->assign('video_info', $video_info);
//        $this->assign('user_info', $user_info);
        $this->assign('comment', $comment);
        return view();
    }

    /**
     * 评论
     * */
    public function comment(){
        try {
            $content = input('param.content');
            $openid = session('openid');//用户openid
            $video_id = input('param.video_id');
            model('zb_comment')->save(['content' => $content, 'openid' => $openid,  'nickname' => session('nickname'),  'headimgurl' =>  session('headimgurl'), 'video_id' => $video_id]);
            return json(['code' => 200]);
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 获取评论列表
     * @return \think\response\Json
     */
    public function get_comment_list() {
        try {
            $type = input('param.type', 'append');
            $id = input('param.id', 0);
            $video_id = input('param.video_id');
            $openid = session('openid');//用户openid
            $is_more = true;
            if ($id > 0) {
                if ($type == 'append') {
                    $data = model('zb_comment')->where('id', '<', $id)->where('video_id', $video_id)->order('id desc')->limit(15)->select();
                    if (count($data) < 15) {
                        $is_more = false;
                    }
                } else {
                    $data = model('zb_comment')->where('id', '>', $id)->where('video_id', $video_id)->order('id desc')->select();

                }
            } else {
                $data = model('zb_comment')->where('video_id', $video_id)->order('id desc')->limit(15)->select();
            }
            if (!$data) {
                throw new \Exception('暂无新的评价内容！' . model('zb_comment')->getLastSql());
            }

            $praise = model('zb_praise')->where('video_id', $video_id)->where('openid', $openid)->column('comment_id');
            $view = new View();
            $view->assign('data', $data);
            $view->assign('praise', $praise);
            $view->assign('openid', $openid);
            $tpl = $view->fetch('comment_list_tpl');

            $comment = model('zb_comment')->where(['video_id'=>$video_id])->count();//评论数

            return json(['code' => 200, 'tpl' => $tpl, 'is_more' => $is_more, 'num' => $comment]);
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 点赞
     * */
    public function zan(){
        try {
            $comment_id = input('param.id');//评论id
            $openid = session('openid');//用户openid
            $video_id = input('param.video_id');

            $comment = model('zb_comment')->get(['id' => $comment_id]);
            if ($comment->openid == $openid) {
                throw new Exception('不能给自己点赞！');
            }

            $again_zan = model('zb_praise')->where(['comment_id'=>$comment_id,'openid'=>$openid])->count();
            if($again_zan > 0){
                throw new Exception('已点赞！');
            }

            $data['comment_id'] = $comment_id;
            $data['openid'] = $openid;
            $data['video_id'] = $video_id;
            model('zb_praise')->save($data);
            model('zb_comment')->where(['id'=>$comment_id])->setInc('praise',1);
            return json(['code' => 200, 'num' => $comment->praise + 1]);
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }
}